import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutternettest/utils/sp_util.dart';
import 'package:select_dialog/select_dialog.dart';

class TestLogin extends StatefulWidget {
  @override
  _TestLoginState createState() => _TestLoginState();
}

class _TestLoginState extends State<TestLogin> {
  var ipAddress;
  var port;
  var rootPath;
  var userName;
  var passWord;
  List<String> ips = [];
  List<String> ports = [];
  List<String> roots = [];
  List<String> usernames = [];

  TextEditingController ipController = TextEditingController();
  TextEditingController portController = TextEditingController();
  TextEditingController rootController = TextEditingController();
  TextEditingController userNameController = TextEditingController();
  TextEditingController passWordController = TextEditingController();
  SPUtil mSu;

  @override
  void initState() {
    super.initState();
    getCachedData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomPadding: false,
        resizeToAvoidBottomInset: false,
        body: Padding(
          padding: EdgeInsets.all(20),
          child: ListView(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      controller: ipController,
                      autofocus: true,
                      decoration: InputDecoration(labelText: "IP地址"),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: GestureDetector(
                      child: Text(
                        "历史",
                        style: TextStyle(color: ips.isEmpty?Colors.grey:Colors.blue, fontSize: 14),
                      ),
                      onTap: () => showIpSelect(ips),
                    ),
                  )
                ],
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      controller: portController,
                      autofocus: true,
                      decoration: InputDecoration(labelText: "端口号"),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: GestureDetector(
                      child: Text(
                        "历史",
                        style: TextStyle(color: ports.isEmpty?Colors.grey:Colors.blue, fontSize: 14),
                      ),
                      onTap: () => showPortSelect(ports),
                    ),
                  )
                ],
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      controller: rootController,
                      autofocus: true,
                      decoration: InputDecoration(labelText: "根目录"),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: GestureDetector(
                      child: Text(
                        "历史",
                        style: TextStyle(color: roots.isEmpty?Colors.grey:Colors.blue, fontSize: 14),
                      ),
                      onTap: () => showRootSelect(roots),
                    ),
                  )
                ],
              ),
              Container(
                height: 100,
              )
              ,
              Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      controller: userNameController,
                      autofocus: true,
                      decoration: InputDecoration(labelText: "用户名"),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: GestureDetector(
                      child: Text(
                        "历史",
                        style: TextStyle(color: usernames.isEmpty?Colors.grey:Colors.blue, fontSize: 14),
                      ),
                      onTap: () => showUserNameSelect(usernames),
                    ),
                  )
                ],
              ),
              TextField(
                controller: passWordController,
                autofocus: true,
                decoration: InputDecoration(labelText: "密码"),
              ),
            ],
          ),
        ));
  }

  void getCachedData() async {
    mSu = await SPUtil.getInstance();
    ips = getSpList("ips");
    ips.add("10.0.22.51");
    ips.add("10.0.22.53");
    ports = getSpList("ports");
    roots = getSpList("roots");
    setState(() {
      ips;
      ports;
      roots;
    });
  }

  List<String> getSpList(String key) {
    if (mSu == null) return [];
    String res = mSu.getString(key);
    if (res != null && res.length > 0) {
      return res.split(",");
    } else {
      return [];
    }
  }

  void addItemToSpList(String key, List<String> list, String newValue) {
    if (!list.contains(newValue)) {
      list.add(newValue);
    }
    saveSpListItem(key, list);
  }

  void saveSpListItem(String key, List<String> newList) {
    String val = newList.join(",");
    if (mSu != null) {
      mSu.putString(key, val);
    }
  }

  showIpSelect(List<String> ips) {
    if (ips.isEmpty) {
      return;
    }
    SelectDialog.showModal<String>(
      context,
      label: "请选择",
      selectedValue: ipAddress,
      items: ips,
      onChange: (String selected) {
        setState(() {
          ipAddress = selected;
        });
        ipController.text = selected;
      },
    );
  }

  showPortSelect(List<String> ports) {
    if (ports.isEmpty) {
      return;
    }
    SelectDialog.showModal<String>(
      context,
      label: "请选择",
      selectedValue: port,
      items: ports,
      onChange: (String selected) {
        setState(() {
          port = selected;
        });
        portController.text = selected;
      },
    );
  }

  showRootSelect(List<String> roots) {
    if (roots.isEmpty) {
      return;
    }
    SelectDialog.showModal<String>(
      context,
      label: "请选择",
      selectedValue: rootPath,
      items: roots,
      onChange: (String selected) {
        setState(() {
          rootPath = selected;
        });
        rootController.text = selected;
      },
    );
  }

  showUserNameSelect(List<String> usernames) {
    if (usernames.isEmpty) {
      return;
    }
    SelectDialog.showModal<String>(
      context,
      label: "请选择",
      selectedValue: userName,
      items: usernames,
      onChange: (String selected) {
        setState(() {
          userName = selected;
        });
        userNameController.text = selected;
      },
    );
  }
}
